<?php

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/03/29
//      |_|                                         | TpMeCMS

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller\user;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use ImiApp\ApiServer\Common\AuthApi;
// use GuzzleHttp\Client;
// use GuzzleHttp\Promise;

use Yurun\Util\HttpRequest;
use Imi\Cache\CacheManager as Cache;


/**
 * 测试用户
 * @Controller("/user.test/")
 */
class Test extends UserBean
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 测试注册用户
     *
     * @param string $invite_code 邀请码默认000Q
     * @param string $password 密码默认123456
     * @param string $mobile 手机号不填默认随机(2填1)
     * @param string $limit  批量生成多少个用户(2填1 优先级最高)
     * @return void
     * @Action
     */
    public function testRegUser($invite_code = null, $password = null, $mobile = null, $limit = 1)
    {
        if (!$limit)
            $limit = 1;
        for ($i = 0; $i < $limit; $i++) {


            if (!$mobile || $i >= 1) {
                $mobile = '19999998' . rand(100, 999);
            }

            if (!$invite_code) {
                $invite_code = '000Q';
            }

            if (!$password) {
                $password = '123456';
            }
            $httpRequest = new HttpRequest();
            // $client = new Client();
            $POST_DATA = [
                'invite_code' => $invite_code,
                'password' => $password,
                'username' => $mobile,
                'mobile' => $mobile,
                'code' => date('md')
            ];

            // $res = $client->request('POST', $this->request->domain().'/api/user/register', $POST_DATA);
            // $res = $client->post($this->request->domain() . '/api/user/register', $POST_DATA);

            $response = $httpRequest->post('http://116.204.112.40:8082/api/user/register', $POST_DATA);
            var_dump($response);
            $json = $response->body();
            $res = json_decode($json, true);
            if ($res['code'] > 0) {
                return $this->success($res['msg'], $res['data']);
            } else {
                return $this->error($res['msg'], $res['data']);
            }

            // echo ($res->getBody()->getContents());
        }
    }


    /**
     * 测试拍卖出价耗时
     *
     * @param int $id 拍卖id
     * @param float $price 价格
     * @return void
     * @Action
     */
    public function testAuctionOfferTime($id = 1, $price = null)
    {
        $s = $this->time();
        for ($i = 0; $i < 10000; $i++) {
            $auction = Db::name('auction')->where('id', '=', $id)->find();
        }
        $e = $this->time();
        var_dump('开始时间' . $s . '结束时间' . $e, '耗时' . strval($e - $s));
        Cache::set('redis', 'auction' . (string)$id, $auction);
        $s = $this->time();
        for ($i = 0; $i < 10000; $i++) {
            $auction = Cache::get('redis', 'auction' . (string)$id);
        }
        $e = $this->time();
        var_dump('开始时间' . $s . '结束时间' . $e, '耗时' . strval($e - $s));
        return $this->success('耗时', $auction);
        // float(16879200173876)
        // float(16879200197211)
        // string(52) "开始时间16879200173876结束时间16879200197211"
        // string(18) "耗时23334.921875"
        // float(16879200197230)
        // float(16879200211088)
        // string(52) "开始时间16879200197231结束时间16879200211088"
        // string(21) "耗时13857.298828125"


    }


    // /**
    //  * 测试拍卖更新拍卖id
    //  *
    //  * @return void
    //  * @Action
    //  */
    // public function testAuctionFree()
    // {
    //     $list = Db::name('freeze')->whereIsNull('auction_id')->select()->getArray();
    //     foreach ($list as $key => $val) {
    //         $auction_id = false;
    //         $auction_id = $val['order_id'];
    //         // switch ($val['type']) {
    //         //     case 'auction_enroll':
    //         //         $auction_id = Db::name('auction_enroll')->where('id', '=', $val['order_id'])->value('auction_id');
    //         //         break;

    //         //     default:

    //         //         if (stripos($val['type'], '_offer') === false) {
    //         //             $auction_id = Db::name('auction_offer')->where('id', '=', $val['order_id'])->value('auction_id');
    //         //         } else {
    //         //             // return $this->success('offer',$val);
    //         //             $auction_id = $val['order_id'];
                       
    //         //         }
    //         //         break;
    //         // }
    //         if ($auction_id) {
    //             Db::name('freeze')->where('id', '=', $val['id'])->update(['auction_id' => $auction_id]);
    //         }
    //     }
    //     return $this->success('list',$list);
    // }

    /**
     * 获取毫秒时间戳
     *
     * @return int
     */
    private function time()
    {
        var_dump(microtime(true) * 10000);
        return microtime(true) * 10000;
    }

    /**
     * 测试拍卖完成创建订单
     *
     * @param int $id 拍卖id
     * @return void
     * @Action
     */
    public function testAuctionOverOrder($id = null)
    {

        $auction = Db::name('auction')->where('id', '=', $id)->find();
        $queue = \Imi\Queue\Facade\Queue::getQueue('auction');
        $message = new \Imi\Queue\Model\Message;
        //发布对列
        $message->setMessage(json_encode(['type' => 'auction_finish3', 'auction' => $auction], JSON_UNESCAPED_UNICODE));
        $queue->push($message);
        return $this->success('ok');
    }


    /**
     * 测试更新拍卖超限高价订单更新队列
     *
     * @return void
     * @Action
     */
    public function testAuctionOverMaxpriceOrder()
    {

        $auction_list = Db::name('auction')->where('max_price_status', '=', '2')->orderRaw('id desc')->select()->getArray();

        $queue = \Imi\Queue\Facade\Queue::getQueue('auction');
        $message = new \Imi\Queue\Model\Message;

        if ($auction_list) {
            foreach ($auction_list as $key => $auction) {
                var_dump('测试更新拍卖超限高价订单更新队列' . $auction['id']);
                //发布对列
                $message->setMessage(json_encode(['type' => 'auction_finish3', 'auction' => $auction], JSON_UNESCAPED_UNICODE));
                $queue->push($message);
            }
        }

        return $this->success('ok');
    }
}
